//
//  LQAFNetworkingTools.swift
//  Weibo
//
//  Created by ios on 16/8/9.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import AFNetworking
//网络请求枚举类型
enum RequestType: Int {
    //  get 请求方式
    case GET = 0
    //  post 请求方式
    case POST = 1
}
//网络请求的类
class LQAFNetworkingTools: AFHTTPSessionManager {
    
    static let sharedTools: LQAFNetworkingTools = {
        let tools = LQAFNetworkingTools()
        //  添加响应可接受的类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    func request(requestType: RequestType, url: String, params: AnyObject?, callBack: (response: AnyObject?, error: NSError?)->()) {
        if requestType == .GET {
            
            GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })

        }else {
            
            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        
        }
        
    }
    
    //封装上传图片的接口
    func requestUpload(url: String, params: AnyObject?,imageData:NSData,name:String ,callBack: (response: AnyObject?, error: NSError?)->()){
     POST(url, parameters: params, constructingBodyWithBlock: { (formDate) -> Void in
        formDate.appendPartWithFileData(imageData, name: name, fileName: "test", mimeType: "application/octet-stream")
        }, progress:nil ,success: { (_, response) -> Void in
           callBack(response: response, error: nil)
        }) { (_, error) -> Void in
            callBack(response: nil, error: error)
        }
    
    }
    
    
}


//  发微博相关接口
extension LQAFNetworkingTools{
    //  发送带图片的微博
    func upadate(access_token: String,status: String ,image: UIImage,callBack: (response: AnyObject?, error: NSError?)->()){
      let url = "https://upload.api.weibo.com/2/statuses/upload.json"
      let params = [
        "access_token": access_token,
        "status": status
        ]
        
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        requestUpload(url, params: params, imageData: imageData, name: "pic", callBack: callBack)
    }
    
    //  发送文字微博
    func update(access_token: String, status: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": access_token,
            "status": status
        ]
        request(.POST, url: url, params: params, callBack: callBack)
    }
}


//  首页相关接口
extension LQAFNetworkingTools{
//获取当前登录用户及其所关注（授权）用户的最新微博
func reqeustStatuses(since_id: Int64 = 0 ,max_id:Int64 = 0, accessToken: String, callBack: (response: AnyObject?, error: NSError?)->()) {
    let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
    let params = [
        "access_token": accessToken,
        "since_id": "\(since_id)",
        "max_id": "\(max_id)"
    ]
//    let path = url + "?access_token=" + accessToken
//    print(path)
    request(.GET, url: url, params: params, callBack: callBack)
}
}
//  OAuth登录相关接口
extension LQAFNetworkingTools{
    //  通过code获取accesstoken
    func reqeustAccessTokenWithCode(code: String, callBack: (response: AnyObject?, error: NSError?)->()) {
    //准备url
    let url = "https://api.weibo.com/oauth2/access_token"
     // 准备参数
    let params = [
        
        "client_id": WeiboAppKey,
        "client_secret": WeiboAppSecret,
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": WeiboRedirect_Uri
        
        ]
        request(.POST, url: url, params: params, callBack: callBack)
    }

    func requestUserInfo(userAccount: LQUserAccount,callBack: (response: AnyObject?, error: NSError?)->()) {
    //  通过accesstoken和uid获取用户信息
        let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        request(.GET, url: url, params: params, callBack: callBack)
    }
}













